<?php
namespace App\Common;

class Curl
{

    public static function requestDataCard($url, $method, $fields = '',$httpHeaders = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36');
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);

        if(is_array($httpHeaders)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }

        if (strpos($url, "https://") !== false )
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        switch ($method) {
            case 'POST':
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
                if (!empty($fields)) {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                }
                break;
            case 'DELETE':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($fields)) {
                    $url = "{$url}?{$fields}";
                }
        }

        $response = curl_exec($ch);

        if ($response == false) {
            curl_close ($ch);
            return curl_error($ch);
        }
        curl_close ($ch);
        return $response;
    }


    public static function requestData($url, $method, $fields = '',$httpHeaders = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        //curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36');
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        
        if(is_array($httpHeaders)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        
        if (strpos($url, "https://") !== false )
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        
        switch ($method) {
            case 'POST':
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
                if (!empty($fields)) {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                }
                break;
            case 'DELETE':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($fields)) {
                    $url = "{$url}?{$fields}";
                }
        }
        
        $response = curl_exec($ch);
        //$request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
        //echo '<pre>';
        //print_r($response);die;
        curl_close ($ch);
        if ($response == false) {
            return curl_error($ch);
        }
        
        return $response;
    }
     
}

?>
